﻿using UnityEngine;
using UnityEngine.UI;
using System;
using System.Collections;
using RestSharp;

using DTFight;
using PuSuoPai;

public class UINBGameMgr : MonoBehaviour
{
	public Sprite[] SptBackgroud = new Sprite[(int)NBGame.MODE_TYPE.MAX];
	public Image ImgBackgroud;

	public Text TxTitle, TxCurPoint, TxSelfTopPoint, TxSelfRank, TxGameTopPoint;
	public Image TopCard, ResultCard1, ResultCard2, ResultCard3;
	public Image[] BlockCards = new Image[9];
	public GameObject ObjStart, ObjRestart;
	public GameObject ObjResult1, ObjResult2, ObjInvalidResult;

	public Button[] BtnLevels = new Button[(int)NBGame.DIFFCULT_TYPE.MAX];
	public GameObject ObjSelectPage;

	bool mbCurPlayState = false;
	public PlayerRecordNB CurPlayerRecord { get; set; }

	// Use this for initialization
	void Start ()
	{
		NBGame.Instance.Init();

		for (int i=0; i<BtnLevels.Length; ++i)
		{
			if (BtnLevels[i] == null)
				continue;

			if (i <= UILogin.CurPlayer.NBDiffcult)
				BtnLevels[i].interactable = true;
			else
				BtnLevels[i].interactable = false;
		}
	}

	// Update is called once per frame
	void Update ()
	{
		NBGame.Instance.Update();

		UpdateUI();
	}

	void UpdateUI()
	{
		if (NBGame.Instance.Mode == NBGame.MODE_TYPE.预测)
		{
			if (ObjSelectPage != null && ObjSelectPage.activeInHierarchy)
				ObjSelectPage.SetActive(false);

			if (ImgBackgroud != null && SptBackgroud[(int)NBGame.MODE_TYPE.预测] != null)
			{
				ImgBackgroud.sprite = SptBackgroud[(int)NBGame.MODE_TYPE.预测];
			}
		}
		else
		{
			if (ImgBackgroud != null && SptBackgroud[(int)NBGame.MODE_TYPE.娱乐] != null)
			{
				ImgBackgroud.sprite = SptBackgroud[(int)NBGame.MODE_TYPE.娱乐];
			}
		}



		if (TxTitle != null)	TxTitle.text = NBGame.Instance.Title;

		if (CurPlayerRecord != null)
		{
			if (TxSelfTopPoint != null)	TxSelfTopPoint.text = CurPlayerRecord.TopScore.ToString();
			if (TxSelfRank != null)
			{
				if(CurPlayerRecord.TopScore > 0)
					TxSelfRank.text = (CurPlayerRecord.TopRank+1).ToString();
				else
					TxSelfRank.text = CurPlayerRecord.TopRank.ToString();
			}
			if (TxGameTopPoint != null)	TxGameTopPoint.text = CurPlayerRecord.TopGameScore.ToString();
		}

		if (NBGame.Instance.IsPlaying)
		{
			mbCurPlayState = true;

			if (ObjStart != null && ObjStart.activeInHierarchy)	ObjStart.SetActive(false);
			if (ObjRestart != null && ObjRestart.activeInHierarchy)	ObjRestart.SetActive(false);

			for (int i=0; i<BlockCards.Length; ++i)
			{
				if (BlockCards[i] == null)
					continue;
				
				if (NBGame.Instance.BlockCards[i].Count > 0)
				{
					if (!BlockCards[i].enabled) BlockCards[i].enabled = true;
					
					byte cardId = NBGame.Instance.BlockCards[i][0];
					if (BlockCards[i].sprite == null || !BlockCards[i].sprite.name.Equals("b" + cardId.ToString()))
						BlockCards[i].sprite = Resources.Load<Sprite> ("Cards_Nine/b"+cardId.ToString());
				}
				else
				{
					if (BlockCards[i].enabled) BlockCards[i].enabled = false;
				}
			}

			if (TopCard != null)
			{
				byte cardId = NBGame.Instance.TopCard;
				if (TopCard.sprite == null || !TopCard.sprite.name.Equals("b"+cardId.ToString()))
					TopCard.sprite = Resources.Load<Sprite> ("Cards_Nine/b"+cardId.ToString());
			}
		}
		else
		{
			if (NBGame.Instance.TopCard == 0)
			{
				if (ObjStart != null && !ObjStart.activeInHierarchy)	ObjStart.SetActive(true);
				if (ObjRestart != null && ObjRestart.activeInHierarchy)	ObjRestart.SetActive(false);
			}
			else
			{
				if (ObjStart != null && ObjStart.activeInHierarchy)	ObjStart.SetActive(false);
				if (ObjRestart != null && !ObjRestart.activeInHierarchy)	ObjRestart.SetActive(true);
			}

			// 显示结果
			if (mbCurPlayState)
			{
				mbCurPlayState = false;

				if (NBGame.Instance.CurCardResult[0] == 0)
				{
					if (ObjResult1 != null && ObjResult1.activeInHierarchy) ObjResult1.SetActive(false);
					if (ObjResult2 != null && ObjResult2.activeInHierarchy) ObjResult2.SetActive(false);
					if (ObjInvalidResult != null && !ObjInvalidResult.activeInHierarchy) ObjInvalidResult.SetActive(true);
				}
				else
				{
					if (ObjInvalidResult != null && ObjInvalidResult.activeInHierarchy) ObjInvalidResult.SetActive(false);

					if (NBGame.Instance.Mode == NBGame.MODE_TYPE.娱乐)
					{
						if (ObjResult1 != null && !ObjResult1.activeInHierarchy) ObjResult1.SetActive(true);
						if (ObjResult2 != null && ObjResult2.activeInHierarchy) ObjResult2.SetActive(false);

						if (ResultCard1 != null)
						{
							ResultCard1.sprite = Resources.Load<Sprite> ("DescCards/a"+NBGame.Instance.CurCardResult[0].ToString());
						}

						if (TxCurPoint != null)	TxCurPoint.text = NBGame.Instance.CurCardResult[0].ToString();

						// update result
						var request = new RestRequest("NineBlocks/Update", Method.POST);
						request.AddParameter("Name", UILogin.CurPlayer.Id); // adds to POST or URL querystring based on Method
						request.AddParameter("Diffcult", ((int)NBGame.Instance.Diffcult).ToString()); 
						request.AddParameter("NewScore", NBGame.Instance.CurCardResult[0].ToString()); 
						IRestResponse<PlayerRecordNB> record = UINetworkMgr.Instance.WebClient.Execute<PlayerRecordNB>(request);

						if (record.Data.TopScore != 0)
						{
							CurPlayerRecord = record.Data;
							UILogin.CurPlayer.NBDiffcult = CurPlayerRecord.TopLevel;
						}

					}
					else if (NBGame.Instance.Mode == NBGame.MODE_TYPE.预测)
					{
						if (ObjResult1 != null && ObjResult1.activeInHierarchy) ObjResult1.SetActive(false);
						if (ObjResult2 != null && !ObjResult2.activeInHierarchy) ObjResult2.SetActive(true);

						if (ResultCard2 != null)
						{
							ResultCard2.sprite = Resources.Load<Sprite> ("DescCards/a"+NBGame.Instance.CurCardResult[0].ToString());
						}

						if (ResultCard3 != null)
						{
							ResultCard3.sprite = Resources.Load<Sprite> ("DescCards/a"+NBGame.Instance.CurCardResult[1].ToString());
						}
					}
				}
			}
		}
	}

	public void SelectBlock(int index)
	{
		NBGame.Instance.SelectBlock(index);
	}

	public void GameStart()
	{
		NBGame.Instance.Start();
	}

	public void RefreshTopCard()
	{
		NBGame.Instance.RefreshTopCard();
	}

	public void SelectLevel(int level)
	{
		if (level < 0 || level >= (int)NBGame.DIFFCULT_TYPE.MAX)
			return;

		NBGame.Instance.Diffcult = (NBGame.DIFFCULT_TYPE)level;

		if (ObjSelectPage != null)
			ObjSelectPage.SetActive(false);

		var request = new RestRequest("NineBlocks", Method.GET);
		request.AddParameter("Name", UILogin.CurPlayer.Id); // adds to POST or URL querystring based on Method
		request.AddParameter("Diffcult", level.ToString()); 
		IRestResponse<PlayerRecordNB> record = UINetworkMgr.Instance.WebClient.Execute<PlayerRecordNB>(request);
		CurPlayerRecord = record.Data;
	}
}

